package org.jash.apps.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.GridLayoutManager.SpanSizeLookup
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.Adapter
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import org.jash.apps.databinding.ItemAppsBinding
import org.jash.apps.databinding.ItemCategoryBinding
import org.jash.apps.entity.AppEntity
import org.jash.apps.entity.CategoryEntity
import org.jash.apps.BR

class CategoryAdapter(val data:MutableList<Any> = mutableListOf()):Adapter<CategoryViewHolder>() {
    var listener:((d:AppEntity) -> Unit)? = null
    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        super.onAttachedToRecyclerView(recyclerView)
        val layoutManager = GridLayoutManager(recyclerView.context, 4).apply {
            spanSizeLookup = object :SpanSizeLookup(){
                override fun getSpanSize(position: Int): Int = when(data[position]) {
                    is CategoryEntity -> 4
                    else -> 1
                }
            }
        }
        recyclerView.layoutManager = layoutManager
    }

    override fun getItemViewType(position: Int): Int = when(data[position]) {
        is CategoryEntity -> 1
        else -> 0
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CategoryViewHolder = CategoryViewHolder(when(viewType) {
        1 -> ItemCategoryBinding.inflate(LayoutInflater.from(parent.context), parent, false).apply {
            this.expanded.setOnCheckedChangeListener { buttonView, isChecked ->
                val binding: ItemCategoryBinding? = DataBindingUtil.bind(buttonView.parent as View)
                binding?.category?.let {
                    val position = data.indexOf(it)
                    if (isChecked) {
                        data.addAll(position + 1, it.appsList)
                        notifyItemRangeInserted(position + 1, it.appsList.size)
                    } else {
                        for (i in 0 until it.appsList.size) {
                            data.removeAt(position + 1)
                        }
                        notifyItemRangeRemoved(position + 1, it.appsList.size)
                    }
                }
            }
        }
        else -> ItemAppsBinding.inflate(LayoutInflater.from(parent.context), parent, false).apply {
            listener?.let { l ->
                this.root.setOnClickListener {
                    val binding:ItemAppsBinding? = DataBindingUtil.bind(it)
                    binding?.app?.let { b -> l(b) }
                }
            }
        }
    })

    override fun getItemCount(): Int = data.size

    override fun onBindViewHolder(holder: CategoryViewHolder, position: Int) {
        val d = data[position]
        when(d) {
            is CategoryEntity -> holder.binding.setVariable(BR.category, d)
            is AppEntity -> holder.binding.setVariable(BR.app, d)
        }
    }
    operator fun plusAssign(list:List<CategoryEntity>) {
        val size = data.size
        list.forEach {
            data += it
            data.addAll(it.appsList)
        }
        notifyItemRangeInserted(size, data.size - size)
    }
    fun setOnClickListener(listener:((d:AppEntity) -> Unit)) {
        this.listener = listener
    }

}
class CategoryViewHolder(val binding:ViewDataBinding):ViewHolder(binding.root)